 #! /bin/bash
VERSION="2.8.3" ##lwjgl
MINEICON="http://media.cdn.ubuntu-de.org/wiki/attachments/56/35/minecraft_logo1.png"
MINEJAR="https://s3.amazonaws.com/MinecraftDownload/launcher/minecraft.jar"

MINEHOME="${HOME}/.minecraft/bin" 
CURDIR="`dirname $0`"

##arch
case `uname -m` in
i686|i586|i486|i386)
ARCH=i386
DMOD=-d32
;;
x86_64|amd64)
ARCH=amd64
DMOD=-d64
;;
*)
echo "Sorry, not supported by minecraft";
exit
;;
esac


#minecraft starter content
##############################################################################
LINE1="#! /bin/bash"
LINE2="export LD_PRELOAD=\"/usr/lib/jvm/default-java/jre/lib/${ARCH}/libjsig.so\""
LINE3="export J2D_PIXMAPS=\"server\""
LINE4="export _JAVA_OPTIONS=\"-Xmx1024M -Xms512M\""
LINE5="java ${DMOD} -cp \"\`dirname \$0\`/minecraft.jar\" net.minecraft.LauncherFrame"
LINE6="exit"
##############################################################################


# create a menu entry for minecraft 
##############################################################################
MENLINE1="[Desktop Entry]"
MENLINE2="Exec=${MINEHOME}/minecraft"
MENLINE3="GenericName=placing blocks to build anything you can imagine"
MENLINE4="Icon=${MINEHOME}/minecraft.png"
MENLINE5="Name=Minecraft"
MENLINE6="Type=Application"
MENLINE7="Categories=Application;Game;"
##############################################################################

case "$@" in
--install)
echo -e "LWJGL version:\t\t${VERSION}"
echo -e "Minecraft directory:\t\t${MINEHOME}"
echo -e "Arch:\t\t\t`uname -m` -> ${ARCH}"
echo "start? [ENTER]/CTRL+C"
read n

TMP="/tmp/minecraft"
mkdir -p ${TMP}
cd ${TMP}
wget -c -O ${TMP}/lwjgl-${VERSION}.zip  http://sourceforge.net/projects/java-game-lib/files/Official%20Releases/LWJGL%20${VERSION}/lwjgl-${VERSION}.zip/download
unzip ${TMP}/lwjgl-${VERSION}.zip

mkdir -p ${MINEHOME}/natives

#copy needet files to the minecraft dir
mv ${TMP}/lwjgl-${VERSION}/jar/jinput.jar ${TMP}/lwjgl-${VERSION}/jar/lwjgl.jar ${TMP}/lwjgl-${VERSION}/jar/lwjgl_util.jar  ${TMP}/lwjgl-${VERSION}/jar/lzma.jar ${MINEHOME}
#now the native stuff


case ${ARCH} in
i386)
mv -f ${TMP}/lwjgl-${VERSION}/native/linux/libjinput-linux.so ${TMP}/lwjgl-${VERSION}/native/linux/liblwjgl.so ${TMP}/lwjgl-${VERSION}/native/linux/libopenal.so ${MINEHOME}/natives/
;;
amd64)
mv -f ${TMP}/lwjgl-${VERSION}/native/linux/libjinput-linux64.so ${TMP}/lwjgl-${VERSION}/native/linux/liblwjgl64.so ${TMP}/lwjgl-${VERSION}/native/linux/libopenal64.so ${MINEHOME}/natives/
;;
*)
echo "something is wrong - exit";
exit
;;
esac

#cleanup 
rm -rf ${TMP}

#download minecraft.jar
cd "${MINEHOME}"
wget -c -O "${MINEHOME}/minecraft.jar" ${MINEJAR}
chmod +x "${MINEHOME}/minecraft.jar"

#create start script
echo ${LINE1} > "${MINEHOME}/minecraft"
echo ${LINE2} >> "${MINEHOME}/minecraft"
echo ${LINE3} >> "${MINEHOME}/minecraft"
echo ${LINE4} >> "${MINEHOME}/minecraft"
echo ${LINE5} >> "${MINEHOME}/minecraft"
echo ${LINE6} >> "${MINEHOME}/minecraft"
chmod +x "${MINEHOME}/minecraft"
clear

#create menu
#first Download the Icon
wget -c -O "${MINEHOME}/minecraft.png"  ${MINEICON}
#create app starter dir
mkdir -p "${HOME}/.local/share/applications"
# create menu starter
echo ${MENLINE1} > "${HOME}/.local/share/applications/mojang-minecraft.desktop"
echo ${MENLINE2} >> "${HOME}/.local/share/applications/mojang-minecraft.desktop"
echo ${MENLINE3} >> "${HOME}/.local/share/applications/mojang-minecraft.desktop"
echo ${MENLINE4} >> "${HOME}/.local/share/applications/mojang-minecraft.desktop"
echo ${MENLINE5} >> "${HOME}/.local/share/applications/mojang-minecraft.desktop"
echo ${MENLINE6} >> "${HOME}/.local/share/applications/mojang-minecraft.desktop"
echo ${MENLINE7} >> "${HOME}/.local/share/applications/mojang-minecraft.desktop"

echo Installed!
read n
exit
;;
--uninstall)
  echo "this will delete \"${MINEHOME}\" with ALL(!!!) files in it!!"
  echo "Uninstall minecraft? [yes]/[no]"
  echo
  read rf
    case ${rf} in
      yes|YES|Yes|Y|y)
      echo "YES"
      ;;
      *)
      echo "NO!"
      echo "[ENTER]" && read b
      exit
      ;;
    esac
  rm -rf "${MINEHOME}" "${HOME}/.local/share/applications/mojang-minecraft.desktop"
;;
*)
if [ -d "${MINEHOME}" ]; then {
  x-terminal-emulator  -e sh -c "${0} --uninstall" 
}
else {
  x-terminal-emulator  -e sh -c "'${0}' --install"
}
fi
exit
;;
esac

